import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import {useRef, useState} from 'react';
import { bucketList, merchantList, merchantUpdate, rewardList, updateReward } from "@/services/bz/api";
import { useRequest } from "@@/plugin-request/request";
import { message } from "antd";

type GithubIssueItem = {
  configCode: string;
  bucket: string;
  target: string;
};

export default () => {
  const actionRef = useRef<ActionType>();
  const [merchantData, setMerchantData] = useState([]);
  const [bucketData, setBucketData] = useState([]);

  useRequest(merchantList, {
    onSuccess: (res) => {
      let data = {};
      //将拿到的返回值遍历
      res.list.map(item => {
        //使用接口返回值的id做为 代替原本的0，1
        data[item.merchantCode] = {
          //使用接口返回值中的overdueValue属性作为原本的text:后面的值
          text: item.merchantCode,
        };
      });
      setMerchantData(data);
    },
  });
  useRequest(bucketList, {
    onSuccess: (res) => {
      let data = {};
      res.map(item => {
        //使用接口返回值的id做为 代替原本的0，1
        data[item] = {
          //使用接口返回值中的overdueValue属性作为原本的text:后面的值
          text: item,
        };
      });
      setBucketData(data);
    },
  });
  const columns: ProColumns<GithubIssueItem>[] = [
    {
      title: '商户号',
      dataIndex: 'clientCode',
      key: 'clientCode',
      valueEnum: merchantData,
      editable:false
    },
    {
      title: '阶段',
      key: 'bucket',
      dataIndex: 'bucket',
      width: 200,
      editable:false,
      valueEnum: bucketData
    },
    {
      title: '目标',
      key: 'target',
      dataIndex: 'target',
      hideInSearch: true,
    },
    {
      title: '完成目标占比',
      key: 'targetFinishValue',
      dataIndex: 'targetFinishValue',
      hideInSearch: true,
      editable:false,
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      width: 200,
      render: (text, record, _, action) => [
        <a
          key="editable"
          onClick={() => {
            action?.startEditable?.(record.id,record);
          }}
        >
          编辑
        </a>
      ],
    },
  ];

  return (
    <ProTable<GithubIssueItem>
      columns={columns}
      actionRef={actionRef}
      cardBordered
      request={async (params: any) => {
        console.log("====",params)
        let request = {
            clientCode:null,
          bucket:null
        };
        if (params.clientCode != undefined && params.clientCode !="") {
          request.clientCode = params.clientCode
        }
        if (params.bucket != undefined && params.bucket !="") {
          request.bucket = params.bucket
        }
        let response = await rewardList(request.clientCode,request.bucket);
        return {data: response.data == null ? [] : response.data};
      }}
      editable={{
        type: 'multiple',
        onSave(params: any,value:any){
          updateReward(value.clientCode,value.bucket,value.targetFinishValue,value.target).then(()=>{
            message.success("修改成功")
            actionRef.current?.reload()
          })
        }
      }}
      columnsState={{
        persistenceKey: 'pro-table-singe-demos',
        persistenceType: 'localStorage',
        onChange(value) {
          console.log('value: ', value);
        },
      }}
      rowKey="id"
      search={{
        labelWidth: 'auto',
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values, type) => {
          console.log("~~~~")
          if (type === 'get') {
            return {
              ...values,
              created_at: [values.startTime, values.endTime],
            };
          }
          return values;
        },
      }}
      dateFormatter="string"
      headerTitle={"绩效列表"}
      options={false}
      revalidateOnFocus={false}
      pagination={{
        showSizeChanger: true,
      }}
    />
  );
};
